﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MS.InformationManagement.Core.Support;
using MS.InformationManagement.Core.TransactionsResponse;
using MS.InformationManagement.DAL.DTOs;

namespace MS.InformationManagement.BLL.DeliveryForms
{
    public class DeliveryHtmlFormCreatorCommand : ICommandWithResponse
    {

        #region Fields

        List<DeliveryFormDTO> _deliveryEntity;
        string _company;
        string _planillaId;

        #endregion

        #region constructors

        public DeliveryHtmlFormCreatorCommand(List<DeliveryFormDTO> deliveryEntity, string companyName, string planillaId)
        {
            _deliveryEntity = deliveryEntity;
            _company = companyName;
            _planillaId = planillaId;
        }

        #endregion

        #region Implemented Methods

        public ITransactionResponse Execute()
        {
            
            DeliveryFormHtmlCreationResponse response = new DeliveryFormHtmlCreationResponse();
            StringBuilder deliveryForm = new StringBuilder();

            //Header
            deliveryForm.Append(getDeliveryFormHeader());

            //Titles
            deliveryForm.Append(getPdfTitles());

            //Rows
            foreach (var row in _deliveryEntity)
                deliveryForm.Append(getFormRow(row));

            response.HtmlResponse = getTable(deliveryForm);
            return response;
        }

        public void Undo()
        {
        }

        #endregion

        #region Private Methods

        private string getTable(StringBuilder sbContent)
        {
            StringBuilder result = new StringBuilder()
            .Append("<Html>")
            .Append("<Body>")
            .Append("<table border='1' width='100%' cellspacing='0'>")
            .Append(sbContent.ToString())
            .Append("</table>")
            .Append("</Body>")
            .Append("</Html>");
            return result.ToString();
        }

        private string getFormRow(DeliveryFormDTO _deliveryFormRow)
        {
            StringBuilder sb = new StringBuilder();
            geTDWithContent(sb, _deliveryFormRow.Id, "right", " width='100px' ");
            geTDWithContent(sb, _deliveryFormRow.ModoEnvio, "left", " width='400px' ");
            geTDWithContent(sb, _deliveryFormRow.Destinatario, "left", " width='700px' ");
            geTDWithContent(sb, _deliveryFormRow.Ciudad, "left", " width='400px' ");
            geTDWithContent(sb, _deliveryFormRow.Direccion, "left", " width='700px' ");
            geTDWithContent(sb, _deliveryFormRow.Peso + "g", "right", " width='300px' ");
            geTDWithContent(sb, string.Format("{0:C}", _deliveryFormRow.Costo.ToString()), "right", " width='300px' ");
            return getTRWithContent(sb.ToString(), string.Empty);
        }

        private string getPdfTitles()
        {
            StringBuilder sb = new StringBuilder();
            geTDWithContent(sb, "ID", "center", " width='100px' ");
            geTDWithContent(sb, "Modo Envio", "center", " width='400px' ");
            geTDWithContent(sb, "Destinatario", "center", " width='700px' ");
            geTDWithContent(sb, "Ciudad", "center", " width='400px' ");
            geTDWithContent(sb, "Dirección", "center", " width='700px' ");
            geTDWithContent(sb, "Peso", "center", " width='300px' ");
            geTDWithContent(sb, "Costo", "center", " width='300px' ");
            return getTRWithContent(sb.ToString(), string.Empty);
        }


        private string getDeliveryFormHeader()
        {
            //Header content
            StringBuilder headerCell = new StringBuilder();
            string cellContent = _company + "<br/>Planilla #" + _planillaId + " "+DateTime.Now;
            geTDWithContent(headerCell, cellContent, "center", "colspan='7'");

            //Header container
            return getTRWithContent(headerCell.ToString(), string.Empty);
        }

        private string getTRWithContent(string content, string properties)
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format("<TR {0}>",properties));
            sb.Append(content);
            sb.Append("</TR>");
            return sb.ToString();
        }

        private void geTDWithContent(StringBuilder sb, string content, string align, string properties)
        {
            sb.Append(string.Format("<TD align='{0}' {1}>", align, properties));
            sb.Append(content);
            sb.Append("</TD>");
        }

        #endregion
    }
}
